/**
 * appItemCtrl.java
 *
 */
package com.navidove.web.manager.ctrl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.navidove.appspotlight.model.bo.AppDictionary;
import com.navidove.appspotlight.model.bo.AppItem;
import com.navidove.appspotlight.model.service.AppDictionaryService;
import com.navidove.appspotlight.model.service.AppItemService;
import com.navidove.web.vo.DisplaytagPaging;

/**
 * appItemCtrl
 *
 * @author $Author$
 * @version $Revision$
 * @date $Date$
 * @url $URL$
 */
@Controller
@RequestMapping(value="/app/appItem")
@SessionAttributes(value={"appItemCtrl.query","appItemCtrl.paging"})
public class AppItemQueryCtrl {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	@Autowired
	private AppItemService appItemService;
	
	@Autowired
	private AppDictionaryService appDictionaryService;
	
	public class Query implements Serializable {
		private static final long serialVersionUID = 351050505286016828L;
		
		private String appName;
		private String owner;
		private Long sourceId;
		private Long categoryId;
		public String getAppName() {
			return appName;
		}
		public void setAppName(String appName) {
			this.appName = appName;
		}
		public String getOwner() {
			return owner;
		}
		public void setOwner(String owner) {
			this.owner = owner;
		}
		public Long getSourceId() {
			return sourceId;
		}
		public void setSourceId(Long sourceId) {
			this.sourceId = sourceId;
		}
		public Long getCategoryId() {
			return categoryId;
		}
		public void setCategoryId(Long categoryId) {
			this.categoryId = categoryId;
		}
		
	}
    
    @ModelAttribute("appItemCtrl.paging")
    public DisplaytagPaging createDisplaytagPaging() {
        return new DisplaytagPaging();
    }
    
    @ModelAttribute("appItemCtrl.query")
    public Query createQuery() {
        return new Query();
    }
    
    @ModelAttribute
    public void reset(Model model) {        
        List<AppDictionary> sourceList = appDictionaryService.getAppDictionaryList(AppDictionary.DICTIONARY_TYPE_SOURCE);
        List<AppDictionary> categoryList = appDictionaryService.getAppDictionaryList(AppDictionary.DICTIONARY_TYPE_CATEGORY);
        
        model.addAttribute("sourceList", sourceList);
        model.addAttribute("categoryList", categoryList);
        model.addAttribute("query", createQuery());
        model.addAttribute("paging", createDisplaytagPaging());         
    }

    @RequestMapping("/init")
    public String init(Model model) {
        return query(model, createQuery(), createDisplaytagPaging());        
    }    
    
    @RequestMapping("/query")
    public String query(Model model, 
            @ModelAttribute("query") Query query,
            @ModelAttribute("paging") DisplaytagPaging paging) {
        
        if (StringUtils.isBlank(paging.getSort())) {
            paging.setSort("appName");
            paging.setAsc(true);
        }
        
        List<AppItem> list = appItemService.getAppItemList(query.getAppName(), query.getOwner(), query.getSourceId(), query.getCategoryId(), null, paging);
        model.addAttribute("appItemList", paging.createPaginatedList(list));
                
        model.addAttribute("appItemCtrl.query", query);
        model.addAttribute("appItemCtrl.paging", paging);
        model.addAttribute("query", query);
        model.addAttribute("paging", paging);        
        return "/app/appItem_list";
    }    
    
    @RequestMapping("/back")
    public String back(Model model,
            @ModelAttribute("appItemCtrl.query") Query query,
            @ModelAttribute("appItemCtrl.paging") DisplaytagPaging paging) {
        
        query = query  != null ? query : createQuery();
        paging = paging != null ? paging : createDisplaytagPaging();
                
        return this.query(model, query, paging);
    }     
}
